package com.cc.controller;

import com.cc.model.EbComment;
import com.cc.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class QianTai {
    @Autowired
    private CommentService commentService;

    @RequestMapping("/qiantai")
    public String index(){
        return "index";
    }

    @RequestMapping("/lists")
    public ModelAndView lists(@RequestParam(value = "page",defaultValue = "1") int page,
                              @RequestParam(value = "pageSize",defaultValue = "3")int pageSize){
        //1.获取新闻表的第一页数据，每页三条
        List<EbComment> comments = commentService.list(page,pageSize);

        //获取最大页数
        int maxPage = commentService.getMaxPage(pageSize);

        //2.将数据（List）和视图（jsp）返回给中央控制器
        ModelAndView mv = new ModelAndView();
        //绑定数据
        mv.addObject("comments",comments);
        //绑定当前页
        mv.addObject("curPage",page);
        //b绑定最大页
        mv.addObject("maxPage",maxPage);
        //绑定视图
        mv.setViewName("guestbook");
        return mv;
    }


    @RequestMapping("/save_commit")
    public String saveCommit(EbComment ebComment){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = sdf.format(date);
        ebComment.setEc_Create_time(timeStr);
        if(ebComment.getEc_nick_name()==null||
                ebComment.getEc_nick_name().equals("")||
                ebComment.getEc_nick_name().trim().length()<0||
                ebComment.getEc_content()==null||
                ebComment.getEc_content().equals("")||
                ebComment.getEc_content().trim().length()<=0||
                ebComment.getEc_Create_time()==null||
                ebComment.getEc_Create_time().equals("")||
                ebComment.getEc_Create_time().trim().length()<0){
            return  "manage/manage-error";
        }
        int result = commentService.save(ebComment);
        if(result<=0){

        }

        return "index";
    }
}
